package com.zhku.laosiji.controller;

import com.zhku.laosiji.pojo.Message;
import com.zhku.laosiji.pojo.User;
import com.zhku.laosiji.service.MessageService;
import com.zhku.laosiji.utils.ResultUtils;
import com.zhku.laosiji.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by LINWENHAO on 2018/9/11.
 */
@Controller
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @RequestMapping("/list")
    public ModelAndView getList(@RequestParam(name = "keyword",defaultValue = "") String keyword,
                                Map map) {
        List<Message> messageList = messageService.findAll(keyword);
        map.put("messageList", messageList);

        return new ModelAndView("message-list", map);
    }

    /**
     * 删除
     * @param mid
     * @return
     */
    @PostMapping("/delMessage")
    @ResponseBody
    public ResultVO delete(@RequestParam("messageId") Integer mid){
        try {
            messageService.delete(mid);

        }catch (Exception e){
            ResultUtils.error(1, e.getMessage());
        }

        return ResultUtils.success();
    }

    /**
     * 回复
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/answer")
    public ModelAndView answer(@RequestParam("id") Integer id
            ,Map map) {
        map.put("id", id);

        return new ModelAndView("message-response",map);
    }

    @RequestMapping("/response")
    @ResponseBody
    public ResultVO response(@RequestParam("id") Integer id,
                             @RequestParam("answer") String answer) {
        if (id == null) {
            return ResultUtils.error(0, "该回复贴不存在");
        }
        Message message = messageService.findByid(id);
        message.setAnswer(answer);
        message.setAnswerdate(new Date());
        message.setAdminId(1);
        message.setAdminName("admin");
        messageService.update(message);

        return ResultUtils.success();
    }

    @RequestMapping("/myMessage")
    public String myMessage(Model model,
                            HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            List<Message> messageList = messageService.selectAllByUser(user.getId());
            model.addAttribute("messageList", messageList);
        }

        return "/user/car/message";
    }

    /**
     * 用户删除
     * @param mid
     * @return
     */
    @GetMapping("/delMessage")
    public String userDelete(@RequestParam("messageId") Integer mid){
        messageService.delete(mid);

        return "redirect:/message/myMessage";
    }

    @RequestMapping("/message-save")
    public String messageSave(@RequestParam("content") String content,
                              HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            Message message = new Message();
            message.setContent(content);
            message.setUserId(user.getId());
            message.setUserName(user.getName());
            message.setCreatedate(new Date());

            messageService.insert(message);
            return "redirect:/message/myMessage";
        }else {
            return "user/join";
        }

    }
}
